package com.nys.cqhttp.message.factory.impl;

import com.alibaba.fastjson.JSONObject;
import com.nys.cqhttp.message.factory.IMessageHandler;
import com.nys.cqhttp.message.pojo.Version;
import com.nys.cqhttp.model.Massage;
import com.nys.cqhttp.param.Context;
import com.nys.cqhttp.util.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * DirectUrlMessageHandler
 *
 * @author nysheng
 * @version 1.0.0
 * @description 服务器
 * @date 2021/12/28 19:53
 */
@Slf4j
public class FWQ5MessageHandler implements IMessageHandler {

  @Override
  public List<String> listHandler(Massage messagePojo, Context context) {
    return Arrays.asList(handler(messagePojo, context));
  }

  @Override
  public String handler(Massage messagePojo, Context context) {
    RestTemplate restTemplate = SpringContextUtil.getBean("restTemplate");
    try {
      JSONObject body =
          restTemplate.getForEntity(messagePojo.getValue(), JSONObject.class).getBody();
      Version data = body.getObject("data", Version.class);
      return "服务器版本: " + data.getLocalVersion() + "\nSteam版本: " + data.getSteamVersion();
    } catch (Exception e) {
      return null;
    }
  }
}
